#!/bin/bash
#
#**************************************************
#Author:                    jiangkang
#Date:                      2024-11-1
#FileName:                  josncpp_install.sh
#Description:               jsoncpp-0.10.7库编译
#**************************************************

install_user_home=$1


# 设置安装日志文件
INSTALL_LOG="install_log.txt"

function InstallG++()
{
    apt update
    apt install -y g++-9
    rm /usr/bin/g++
    ln -s /usr/bin/g++-9 /usr/bin/g++
}



function CheckEnvironment()
{
    # 1.检查git是否安装，版本不低于2.25.1
    git --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "git未安装，安装git"
        apt update
        apt install -y git
    fi

    # 2.检查cmake是否安装，并且版本不低于2.8.5
    cmake --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "cmake未安装，安装cmake"
        apt update
        apt install -y cmake
    fi

    # 3.检查make是否安装，并且版本不低于4.2.1
    make --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "make未安装，安装make"
        apt update
        apt install -y make
    fi

    # 4.检查g++是否安装，并且版本是否是g++-9
    g++ --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "g++未安装，安装g++"
        InstallG++
    else
        g++ --version | grep 9 > /dev/null
        if [ $? -ne 0 ]; then
            echo "g++版本不为g++-9，重新安装g++-9"
            InstallG++;
        fi
    fi

    # 5.检查tar命令是否安装
    tar --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "tar命令未安装，安装tar"
        apt update
        apt install -y tar
    fi

    # 6.检查tee命令是否安装
    tee --version > /dev/null
    if [ $? -ne 0 ]; then
        echo "tee命令未安装，安装tee"
        apt update
        apt install -y tee
    fi
    # 7.检查是否安装过jsoncpp
    dpkg -l | grep jsoncpp > /dev/null
    if [ $? -eq 0 ]; then
        for var in $(dpkg -l | grep jsoncpp | awk '{print $2}'); do
            apt remove -y "${var}"
        done
    fi

}

function ConfigureSearchPath()
{
    LIB_PATH=$(grep "libjsoncpp.a" "$INSTALL_LOG" | sed -E 's|.*: ([^ ]+/).*|\1|')
    INCLUDE_PATH='/usr/local/include/jsoncpp'
    sed -i "\$a\ export LIBRARY_PATH=$LIBRARY_PATH:${LIB_PATH}" /etc/profile
    sed -i "\$a\ export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${INCLUDE_PATH}" /etc/profile
    source /etc/profile
    rm -rf ${INSTALL_LOG}
}

function Install()
{
    # 0.克隆仓库
    git clone https://gitee.com/lpsdz-ybhdsg-jk/jsoncpp_install.git
    # 1.进入目录
    cd jsoncpp_install
    # 2.解压
    tar -xf jsoncpp.tar.tz
    # 3.进入目录
    cd jsoncpp
    # 4.切换版本为0.10.7
    git tag > /dev/null
    if [ $? -ne 0 ]; then
        echo "配置git信任目录"
        git config --global --add safe.directory $(pwd)
        #git config --global --add safe.directory /root/jsoncpp_install/jsoncpp
    fi
    git checkout 0.10.7
    # 5.编译
    cmake CMakeLists.txt
    make
    # 6.移动目录
    make install | tee ${INSTALL_LOG}
    # 7.配置搜索路径
    ConfigureSearchPath;
}

function RemoveTrace()
{
    if [ -f "${install_user_home}/install.sh" ]; then
        rm -rf "${install_user_home}/install.sh"
    fi
    if [ -d "${install_user_home}/jsoncpp_install" ];then
        rm -rf "${install_user_home}/jsoncpp_install"
    fi
}


CheckEnvironment;
Install;
RemoveTrace;
